/**********************************************************************************
  Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.

  (c) Copyright 1996 - 2002  Gary Henderson (gary.henderson@ntlworld.com),
                             Jerremy Koot (jkoot@snes9x.com)

  (c) Copyright 2002 - 2004  Matthew Kendora

  (c) Copyright 2002 - 2005  Peter Bortas (peter@bortas.org)

  (c) Copyright 2004 - 2005  Joel Yliluoma (http://iki.fi/bisqwit/)

  (c) Copyright 2001 - 2006  John Weidman (jweidman@slip.net)

  (c) Copyright 2002 - 2006  funkyass (funkyass@spam.shaw.ca),
                             Kris Bleakley (codeviolation@hotmail.com)

  (c) Copyright 2002 - 2007  Brad Jorsch (anomie@users.sourceforge.net),
                             Nach (n-a-c-h@users.sourceforge.net),
                             zones (kasumitokoduck@yahoo.com)

  (c) Copyright 2006 - 2007  nitsuja


  BS-X C emulator code
  (c) Copyright 2005 - 2006  Dreamer Nom,
                             zones

  C4 x86 assembler and some C emulation code
  (c) Copyright 2000 - 2003  _Demo_ (_demo_@zsnes.com),
                             Nach,
                             zsKnight (zsknight@zsnes.com)

  C4 C++ code
  (c) Copyright 2003 - 2006  Brad Jorsch,
                             Nach

  DSP-1 emulator code
  (c) Copyright 1998 - 2006  _Demo_,
                             Andreas Naive (andreasnaive@gmail.com)
                             Gary Henderson,
                             Ivar (ivar@snes9x.com),
                             John Weidman,
                             Kris Bleakley,
                             Matthew Kendora,
                             Nach,
                             neviksti (neviksti@hotmail.com)

  DSP-2 emulator code
  (c) Copyright 2003         John Weidman,
                             Kris Bleakley,
                             Lord Nightmare (lord_nightmare@users.sourceforge.net),
                             Matthew Kendora,
                             neviksti


  DSP-3 emulator code
  (c) Copyright 2003 - 2006  John Weidman,
                             Kris Bleakley,
                             Lancer,
                             z80 gaiden

  DSP-4 emulator code
  (c) Copyright 2004 - 2006  Dreamer Nom,
                             John Weidman,
                             Kris Bleakley,
                             Nach,
                             z80 gaiden

  OBC1 emulator code
  (c) Copyright 2001 - 2004  zsKnight,
                             pagefault (pagefault@zsnes.com),
                             Kris Bleakley,
                             Ported from x86 assembler to C by sanmaiwashi

  SPC7110 and RTC C++ emulator code
  (c) Copyright 2002         Matthew Kendora with research by
                             zsKnight,
                             John Weidman,
                             Dark Force

  S-DD1 C emulator code
  (c) Copyright 2003         Brad Jorsch with research by
                             Andreas Naive,
                             John Weidman

  S-RTC C emulator code
  (c) Copyright 2001-2006    byuu,
                             John Weidman

  ST010 C++ emulator code
  (c) Copyright 2003         Feather,
                             John Weidman,
                             Kris Bleakley,
                             Matthew Kendora

  Super FX x86 assembler emulator code
  (c) Copyright 1998 - 2003  _Demo_,
                             pagefault,
                             zsKnight,

  Super FX C emulator code
  (c) Copyright 1997 - 1999  Ivar,
                             Gary Henderson,
                             John Weidman

  Sound DSP emulator code is derived from SNEeSe and OpenSPC:
  (c) Copyright 1998 - 2003  Brad Martin
  (c) Copyright 1998 - 2006  Charles Bilyue'

  SH assembler code partly based on x86 assembler code
  (c) Copyright 2002 - 2004  Marcus Comstedt (marcus@mc.pp.se)

  2xSaI filter
  (c) Copyright 1999 - 2001  Derek Liauw Kie Fa

  HQ2x, HQ3x, HQ4x filters
  (c) Copyright 2003         Maxim Stepin (maxim@hiend3d.com)

  Win32 GUI code
  (c) Copyright 2003 - 2006  blip,
                             funkyass,
                             Matthew Kendora,
                             Nach,
                             nitsuja

  Mac OS GUI code
  (c) Copyright 1998 - 2001  John Stiles
  (c) Copyright 2001 - 2007  zones


  Specific ports contains the works of other authors. See headers in
  individual files.


  Snes9x homepage: http://www.snes9x.com

  Permission to use, copy, modify and/or distribute Snes9x in both binary
  and source form, for non-commercial purposes, is hereby granted without
  fee, providing that this license information and copyright notice appear
  with all copies and any derived work.

  This software is provided 'as-is', without any express or implied
  warranty. In no event shall the authors be held liable for any damages
  arising from the use of this software or it's derivatives.

  Snes9x is freeware for PERSONAL USE only. Commercial users should
  seek permission of the copyright holders first. Commercial use includes,
  but is not limited to, charging money for Snes9x or software derived from
  Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
  using Snes9x as a promotion for your commercial product.

  The copyright holders request that bug fixes and improvements to the code
  should be forwarded to them so everyone can benefit from the modifications
  in future versions.

  Super NES and Super Nintendo Entertainment System are trademarks of
  Nintendo Co., Limited and its subsidiary companies.
**********************************************************************************/

/**********************************************************************************
  Snes9x GTK Port
  
  (c) Copyright 2007 - 2008  Brandon Wright (bearoso@gmail.com)
**********************************************************************************/

#include <fcntl.h>
#ifdef USE_JOYSTICK
#   include "SDL.h"
#endif

#include "gtk_s9xcore.h"
#include "gtk_s9x.h"
#include "gtk_config.h"
#include "gtk_control.h"

const BindingLink b_links[] = 
{
        /* Joypad-specific bindings. "Joypad# " will be prepended */
        { "toggle_left",      "Left"        },
        { "toggle_right",     "Right"       },
        { "toggle_up",        "Up"          },
        { "toggle_down",      "Down"        },
        { "toggle_start",     "Start"       },
        { "toggle_select",    "Select"      },
        { "toggle_a",         "A"           },
        { "toggle_b",         "B"           },
        { "toggle_x",         "X"           },
        { "toggle_y",         "Y"           },
        { "toggle_l",         "L"           },
        { "toggle_r",         "R"           },
        { "toggle_a_turbo",   "Turbo A"     },
        { "toggle_b_turbo",   "Turbo B"     },
        { "toggle_x_turbo",   "Turbo X"     },
        { "toggle_y_turbo",   "Turbo Y"     },
        { "toggle_l_turbo",   "Turbo L"     },
        { "toggle_r_turbo",   "Turbo R"     },
        { "toggle_a_sticky",  "Sticky A"    },
        { "toggle_b_sticky",  "Sticky B"    },
        { "toggle_x_sticky",  "Sticky X"    },
        { "toggle_y_sticky",  "Sticky Y"    },
        { "toggle_l_sticky",  "Sticky L"    },
        { "toggle_r_sticky",  "Sticky R"    },
        
        /* Emulator based bindings */
        { "toggle_enable_turbo",        "EmuTurbo"          },
        { "toggle_toggle_turbo",        "ToggleEmuTurbo"    },
        { "toggle_pause",               "GTK_pause"         },
        { "toggle_decrease_frame_rate", "DecFrameRate"      },
        { "toggle_increase_frame_rate", "IncFrameRate"      },
        { "toggle_decrease_frame_time", "DecFrameTime"      },
        { "toggle_increase_frame_time", "IncFrameTime"      },
        { "toggle_hardware_reset",      "Reset"             },
        { "toggle_soft_reset",          "SoftReset"         },
        { "toggle_bg_layer_0",          "ToggleBG0"         },
        { "toggle_bg_layer_1",          "ToggleBG1"         },
        { "toggle_bg_layer_2",          "ToggleBG2"         },
        { "toggle_bg_layer_3",          "ToggleBG3"         },
        { "toggle_sprites",             "ToggleSprites"     },
        { "toggle_bg_layering_hack",    "BGLayeringHack"    },
        { "toggle_screenshot",          "Screenshot"        },
        { "toggle_fullscreen",          "GTK_fullscreen"    },
        { "toggle_save_0",              "QuickSave000"      },
        { "toggle_save_1",              "QuickSave001"      },
        { "toggle_save_2",              "QuickSave002"      },
        { "toggle_save_3",              "QuickSave003"      },
        { "toggle_save_4",              "QuickSave004"      },
        { "toggle_save_5",              "QuickSave005"      },
        { "toggle_save_6",              "QuickSave006"      },
        { "toggle_save_7",              "QuickSave007"      },
        { "toggle_save_8",              "QuickSave008"      },
        { "toggle_load_0",              "QuickLoad000"      },
        { "toggle_load_1",              "QuickLoad001"      },
        { "toggle_load_2",              "QuickLoad002"      },
        { "toggle_load_3",              "QuickLoad003"      },
        { "toggle_load_4",              "QuickLoad004"      },
        { "toggle_load_5",              "QuickLoad005"      },
        { "toggle_load_6",              "QuickLoad006"      },
        { "toggle_load_7",              "QuickLoad007"      },
        { "toggle_load_8",              "QuickLoad008"      },
        { "toggle_sound_channel_0",     "SoundChannel0"     },
        { "toggle_sound_channel_1",     "SoundChannel1"     },
        { "toggle_sound_channel_2",     "SoundChannel2"     },
        { "toggle_sound_channel_3",     "SoundChannel3"     },
        { "toggle_sound_channel_4",     "SoundChannel4"     },
        { "toggle_sound_channel_5",     "SoundChannel5"     },
        { "toggle_sound_channel_6",     "SoundChannel6"     },
        { "toggle_sound_channel_7",     "SoundChannel7"     },
        { "toggle_all_sound_channels",  "SoundChannelsOn"   },
        { "toggle_sound_interpolation", "InterpolateSound"  },
        { "toggle_save_spc",            "GTK_save_spc"      },
        { "toggle_open_rom",            "GTK_open_rom"      },
        { "toggle_begin_recording_movie", "BeginRecordingMovie" },
        { "toggle_stop_recording_movie", "EndRecordingMovie" },
        { "toggle_load_movie",          "LoadMovie" },
        { "toggle_seek_to_frame",       "GTK_seek_to_frame" },
        
        { NULL, NULL }
};

bool 
S9xPollButton (uint32 id, bool *pressed)
{
    return true;
}

bool 
S9xPollAxis (uint32 id, int16 *value)
{
    return true;
}

bool 
S9xPollPointer (uint32 id, int16 *x, int16 *y)
{
    *x = top_level->mouse_loc_x;
    *y = top_level->mouse_loc_y; 
    
    return true;
}

void
S9xHandlePortCommand (s9xcommand_t cmd, int16 data1, int16 data2)
{
    if (data1 == FALSE) /* Release */
    {
        if (cmd.port[0] == PORT_COMMAND_FULLSCREEN)
        {
            top_level->toggle_fullscreen_mode ();
        }
        
        else if (cmd.port[0] == PORT_COMMAND_SAVE_SPC)
        {
            top_level->leave_fullscreen_mode ();
            top_level->save_spc_dialog ();
        }
        
        else if (cmd.port[0] == PORT_OPEN_ROM)
        {
            top_level->leave_fullscreen_mode ();
            top_level->open_rom_dialog ();
        }
        
        else if (cmd.port[0] == PORT_PAUSE)
        {
            if (!(top_level->user_pause))
                top_level->pause_from_user ();
            else
                top_level->unpause_from_user ();
        }
        
        else if (cmd.port[0] == PORT_SEEK_TO_FRAME)
        {
            top_level->movie_seek_dialog ();
        }
        
    }
    
    return;
}

Binding
S9xGetBindingByName (const char *name)
{
    for (int i = 0; i < NUM_EMU_LINKS; i++)
    {
        if (!strcasecmp (b_links[i + NUM_JOYPAD_LINKS].snes9x_name, name))
        {
            return gui_config->shortcut[i];
        }
    }
    
    return 0;
}

s9xcommand_t
S9xGetPortCommandT (const char *name)
{
    s9xcommand_t cmd;
    
    cmd.type = S9xButtonPort;
    cmd.multi_press = 0;
    cmd.button_norpt = 0;
    cmd.port[0] = 0;
    cmd.port[1] = 0;
    cmd.port[2] = 0;
    cmd.port[3] = 0;
    
    if (!strcasecmp (name, "GTK_fullscreen"))
    {
        cmd.port[0] = PORT_COMMAND_FULLSCREEN; 
    }
    
    else if (!strcasecmp (name, "GTK_save_spc"))
    {
        cmd.port[0] = PORT_COMMAND_SAVE_SPC;
    }
    
    else if (!strcasecmp (name, "GTK_open_rom"))
    {
        cmd.port[0] = PORT_OPEN_ROM;
    }
    
    else if (!strcasecmp (name, "GTK_pause"))
    {
        cmd.port[0] = PORT_PAUSE;
    }
        
    else if (!strcasecmp (name, "GTK_seek_to_frame"))
    {
        cmd.port[0] = PORT_SEEK_TO_FRAME;
    }
    
    else
    {
        cmd = S9xGetCommandT (name);
    }
    
    return cmd;
}

void
S9xProcessEvents (bool8 block)
{    
#ifdef USE_JOYSTICK
    JoyEvent event;
    Binding binding;
        
    for (int i = 0; gui_config->joystick[i]; i++)
    {
        while (gui_config->joystick[i]->get_event (&event))
        {
            binding = BINDING_JOY;
            binding |= JOY_DEVICE_MASK (i + 1);
            binding |= event.parameter;
            
            S9xReportButton (binding, event.state == JOY_PRESSED ? 1 : 0);
        }
    }
#endif
    
    return;
}

#ifdef USE_JOYSTICK
static void
poll_joystick_events (void)
{
    SDL_Event event;
    
    while (SDL_PollEvent (&event))
    {
        if (event.type == SDL_JOYAXISMOTION)
        {
             gui_config->joystick[event.jaxis.which]->handle_event (&event);
        }
        
        else if (event.type == SDL_JOYBUTTONUP ||
                 event.type == SDL_JOYBUTTONDOWN)
        {
            gui_config->joystick[event.jbutton.which]->handle_event (&event);
        }
    }
    
    return;
}
#endif

void
S9xInitInputDevices (void)
{
#ifdef USE_JOYSTICK
    SDL_Init (SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
    
    int i;
        
    for (i = 0; ; i++)
    {
        gui_config->joystick = (JoyDevice **) 
            realloc (gui_config->joystick,
                     sizeof (JoyDevice *) * (i + 1));
        
        gui_config->joystick[i] = new JoyDevice (i);
        
        if (!gui_config->joystick[i]->enabled)
        {
            delete gui_config->joystick[i];
            gui_config->joystick[i] = NULL;
            break;
        }
        else
        {
            gui_config->joystick[i]->joynum = i;
        }
    }
#endif
    
    return;
}

void
S9xDeinitInputDevices (void)
{
#ifdef USE_JOYSTICK
    int i;
    
    for (i = 0; gui_config->joystick[i] != NULL; i++)
    {
        delete gui_config->joystick[i];
    }
    
    free (gui_config->joystick);
    
    SDL_Quit ();
#endif
    
    return;
}

#ifdef USE_JOYSTICK
JoyDevice::JoyDevice (unsigned int device_num)
{    
    enabled = false;
    queue_head = 0;
    queue_tail = 0;
    axis = NULL;
    filedes = NULL;
    mode = JOY_MODE_INDIVIDUAL;
    
    if ((int) device_num >= SDL_NumJoysticks ())
        return;
    
    filedes = SDL_JoystickOpen (device_num);
    
    if (!filedes)
        return;
    
    enabled = true;
    
    num_axes = SDL_JoystickNumAxes (filedes);
    axis = (int *) malloc (sizeof (int) * num_axes);
    calibration = (Calibration *) malloc (sizeof (Calibration) * num_axes);
    
    for (int i = 0; i < num_axes; i++)
    {
        calibration[i].min = -32767;
        calibration[i].max = 32767;
        calibration[i].center = 0;
    }
    
    memset (axis, 0, sizeof (int) * num_axes);
    
    return;
}

JoyDevice::~JoyDevice (void)
{
    if (enabled)
    {
        SDL_JoystickClose (filedes);
        free (axis);
        free (calibration);
    }
    
    enabled = false;
    
    return;
}

void
JoyDevice::add_event (int parameter, int state)
{
    if ((queue_tail + 1) % 100 == queue_head)
        return; /* Queue full, sort of */
    
    queue[queue_tail].parameter = parameter;
    queue[queue_tail].state = state;
    
    queue_tail = (queue_tail + 1) % 100;
    
    return;
}

void
JoyDevice::register_centers (void)
{
    for (int i = 0; i < num_axes; i++)
    {
        calibration[i].center = SDL_JoystickGetAxis (filedes, i);
    }
    
    return;
}

void
JoyDevice::handle_event (SDL_Event *event)
{
    if (event->type == SDL_JOYAXISMOTION)
    {        
        int cal_min = calibration[event->jaxis.axis].min; 
        int cal_max = calibration[event->jaxis.axis].max;
        int cal_cen = calibration[event->jaxis.axis].center;
        int t = gui_config->joystick_threshold;
        
        int ax_min = (cal_min - cal_cen) * t / 100 + cal_cen;             
        int ax_max = (cal_max - cal_cen) * t / 100 + cal_cen; 
        
        if (mode == JOY_MODE_INDIVIDUAL)
        {
            for (int i = 0; i < NUM_JOYPADS; i++)
            {
                Binding *pad = (Binding *) &(gui_config->pad[i]); 
                
                for (int j = 0; j < NUM_JOYPAD_LINKS; j++)
                {
                    if (JOY_AXIS_UNMASK (pad[j]) == event->jaxis.axis &&
                        JOY_DEVICE_UNMASK (pad[j] == joynum))
                    {
                        t = (int) THRESHOLD_UNMASK (pad[j]);
                        
                        if (JOY_DIRECTION_UNMASK (pad[j]) == AXIS_POS)
                        {
                            ax_max = (cal_max - cal_cen) * t / 100 + cal_cen;
                        }
                        else if (JOY_DIRECTION_UNMASK (pad[j]) == AXIS_NEG)
                        {
                            ax_min = (cal_min - cal_cen) * t / 100 + cal_cen;
                        }
                    }
                }
            }
            
            for (int i = 0; i < NUM_EMU_LINKS; i++)
            {
                if (JOY_AXIS_UNMASK (gui_config->shortcut[i]) == 
                    event->jaxis.axis &&
                    JOY_DEVICE_UNMASK (gui_config->shortcut[i]) ==
                    joynum)
                {
                    if (JOY_DIRECTION_UNMASK (gui_config->shortcut[i]) == 
                        AXIS_POS)
                    {
                        ax_max = (cal_max - cal_cen) * 
                            THRESHOLD_UNMASK (gui_config->shortcut[i]) / 100 + cal_cen;
                    }
                    else if (JOY_DIRECTION_UNMASK (gui_config->shortcut[i]) == 
                             AXIS_NEG)
                    {
                        ax_min = (cal_min - cal_cen) * 
                            THRESHOLD_UNMASK (gui_config->shortcut[i]) / 100 + cal_cen;
                    }
                }
            }
        }
        else if (mode == JOY_MODE_CALIBRATE)
        {
            if (event->jaxis.value < calibration[event->jaxis.axis].min)
                calibration[event->jaxis.axis].min = event->jaxis.value;
            if (event->jaxis.value > calibration[event->jaxis.axis].max)
                calibration[event->jaxis.axis].min = event->jaxis.value;
        }
        
        /* Sanity Check */
        if (ax_min >= cal_cen)
            ax_min = cal_cen - 1;
        if (ax_max <= cal_cen)
            ax_max = cal_cen + 1;

        if (event->jaxis.value <= ax_min &&
                axis[event->jaxis.axis] > ax_min)
        {
            add_event (JOY_AXIS (event->jaxis.axis, AXIS_NEG), 1);
        }

        if (event->jaxis.value > ax_min &&
                axis[event->jaxis.axis] <= ax_min)
        {
            add_event (JOY_AXIS (event->jaxis.axis, AXIS_NEG), 0);
        }

        if (event->jaxis.value >= ax_max &&
                axis[event->jaxis.axis] < ax_max)
        {
            add_event (JOY_AXIS (event->jaxis.axis, AXIS_POS), 1);
        }

        if (event->jaxis.value < ax_max &&
                axis[event->jaxis.axis] >= ax_max)
        {
            add_event (JOY_AXIS (event->jaxis.axis, AXIS_POS), 0);
        }

        axis[event->jaxis.axis] = event->jaxis.value;

    }

    else if (event->type == SDL_JOYBUTTONUP ||
            event->type == SDL_JOYBUTTONDOWN)
    {
        add_event (event->jbutton.button,
                   event->jbutton.state == SDL_PRESSED ? 1 : 0);
    }
    
    return;
}

int
JoyDevice::get_event (JoyEvent *event)
{
    poll_events ();
    
    if (queue_head == queue_tail) /* Queue empty */
        return 0;
    
    event->parameter = queue[queue_head].parameter;
    event->state     = queue[queue_head].state;
    
    queue_head = (queue_head + 1) % 100;
    
    return 1;
}

void
JoyDevice::poll_events (void)
{
    poll_joystick_events ();
    
    return;
}

void
JoyDevice::flush (void)
{
    SDL_Event event;
    
    while (SDL_PollEvent (&event));
    
    queue_head = queue_tail;
    
    return;
}
#endif
